// pages/pay/pay.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsList: [], //商品列表
        totalData: 0, //总金额
        location: {
            name: "",
            phone: "",
            take: "",
            place: ""
        }, //收货地址
        fla: true,
    },
    // 计算总价格
    getTotal() {
        let goodsList = this.data.goodsList;
        if (goodsList) {
            let data = goodsList.reduce(function (total, item) {
                return total + item.num * item.goodsPrice;
            }, 0)
            this.setData({
                totalData: data
            })
        }
    },

    // 支付跳转
    goOrder() {
        if (this.data.fla) {
            wx.navigateTo({
                url: '/pages/order/order?index=2',
            })
            this.setOrderLists(2);
        } else {
            wx.showToast({
                title: "请添加地址",
                icon:"error",
                duration: 2000
            })
            return;
        }
    },
    setOrderLists(type) {
        // 没有数据时候不添加订单
        let goodsCarlistData = wx.getStorageSync("goodsCarList");
        if (!goodsCarlistData) {
            return;
        }
        let orderData = {
            orderId: new Date().getTime(),
            orderLists: null,
            type, //1:待支付 2:待发货 3:待收货 4:待评价
            total: this.data.totalData
        };
        // 添加商品列表到订单中
        orderData.orderLists = this.data.goodsList;
        // 1.判断是否有订单
        let orderLists = wx.getStorageSync('orderLists');
        if (orderLists) {
            orderLists.push(orderData);
            wx.setStorageSync('orderLists', orderLists);
        } else {
            wx.setStorageSync('orderLists', [orderData])
        }
        wx.removeStorageSync('goodsCarList')
    },
    // 切换地址
    switchover() {
        wx.navigateTo({
            url: '/pages/switchover/switchover',
        });
    },
    //获取收货地址
    gettingData() {
        let location = wx.getStorageSync('locationLists')[0];
        this.setData({
            location
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.gettingData();
        let location = wx.getStorageSync('locationLists');
        if (location) {
            this.setData({
                fla: true
            })
        } else {
            this.setData({
                fla: false
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var getCartData = wx.getStorageSync('goodsCarList');
        this.setData({
            goodsList: getCartData
        });
        this.getTotal();
        this.gettingData();
        let location = wx.getStorageSync('locationLists');
        console.log();
        if (location) {
            this.setData({
                fla: true
            })
        } else {
            this.setData({
                fla: false
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        // 请求购物车数据
        // wx.removeStorageSync("goodsCarlist")
        this.setOrderLists(1);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})